package com.xiaoma.myaudience.biz.task;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.xiaoma.myaudience.biz.model.CategoryOptionInfo;
import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.biz.util.VideoPrefHelper;
import com.xiaoma.myaudience.util.HttpUtils;
import com.xiaoma.myaudience.util.Logger;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask;

public class CategoryOptionInfoTask extends BaseDataAsyncTask<String, Void, Void> {
    private final static String TAG = "CategoryOptionInfoTask";

    /**
     * {@inheritDoc}
     * 
     * @param context
     * @param taskType
     * @param callback
     */
    public CategoryOptionInfoTask(Context context, int taskType, DataAsyncCallback callback) {
        super(context, taskType, callback);
    }

    private String getCategoryOptionsUrl(String type) {
		return String.format(Constant.URL_CATEGORY_OPTIONS, type);
    }
    
    @Override
    protected Void doInBackground(String... params) {
        try {
            if (params != null && params.length > 0) {
                for (String category : params) {
                    doGetCategory(category);
                }
            }
        } catch (Exception e) {
            Logger.e(TAG, "doInBackground error!!!", e);
        } finally {
            closeHttpClient();
        }
        return null;
    }
    
    
    private void doGetCategory(String category) {
        String url = getCategoryOptionsUrl(category);
        Logger.i("url is " + url);

        if (!TextUtils.isEmpty(url)) {

            JSONObject jsonObj = HttpUtils.getHttpJSONObjectResult(getHttpClient(), url, null,
                    HttpUtils.GET, mContext);
            if (jsonObj != null) {
//                VideoPrefHelper.putString(mContext, category + CategoryActivity.OPTION_TAG, jsonObj
//                        .toString());
            }
        }
    }
    
    
    public static ArrayList<CategoryOptionInfo> jsonObject2CategoryOptionInfoList(JSONObject jsonObj) {
        ArrayList<CategoryOptionInfo> list = new ArrayList<CategoryOptionInfo>();
        CategoryOptionInfo info;
        JSONArray jsonArray = null;
        
        try {
			if(jsonObj.has(CategoryOptionInfo.PARAM_TYPE)) {
				jsonArray = jsonObj.getJSONArray(CategoryOptionInfo.PARAM_TYPE);
				int size = jsonArray.length();
				for(int i=0; i<size; i++) {
					info = new CategoryOptionInfo(-1, null, null, null);
			        list.add((CategoryOptionInfo) CategoryOptionInfo.jsonObject2CategoryOptionInfo(
			                jsonArray.getJSONObject(i), info));
				}
			}
			if(jsonObj.has(CategoryOptionInfo.PARAM_AREA)) {
				jsonArray = jsonObj.getJSONArray(CategoryOptionInfo.PARAM_AREA);
				int size = jsonArray.length();
				for(int i=0; i<size; i++) {
					info = new CategoryOptionInfo(-1, null, null, null);
			        list.add((CategoryOptionInfo) CategoryOptionInfo.jsonObject2CategoryOptionInfo(
			                jsonArray.getJSONObject(i), info));
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;

    }
}
